package com.yinhe.music.yhmusic.dialog.pay;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceDialogFragment;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.model.ProductInfo;
import com.yinhe.music.yhmusic.pay.PayActivity;
import com.yinhe.music.yhmusic.utils.ViewUtils;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 创建歌单弹窗
 */
public class PayDialog extends BaseServiceDialogFragment implements IPayDialogContract.IPayDialogView {

    private Context mContext;
    private String mSongId;

    @BindView(R.id.price)
    TextView mPriceView;
    @BindView(R.id.balance)
    TextView mBalanceView;
    @BindView(R.id.btn_buy)
    Button mBuyButton;

    Unbinder unbinder;

    PayDialogPresenter mPresenter;

    public static PayDialog newInstance(String songId) {

        Bundle args = new Bundle();
        args.putString("songId", songId);

        PayDialog fragment = new PayDialog();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new PayDialogPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.CustomDatePickerDialog);
        mContext = getContext();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initWindows();
        View view = inflater.inflate(R.layout.dialog_pay, container);
        unbinder = ButterKnife.bind(this, view);
        RxBus.get().register(this);
        if (getArguments() != null) {
            mSongId = getArguments().getString("songId");
        }

        mPresenter.getProductInfo(mSongId);
        return view;
    }

    private void initWindows() {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setAttributes(params);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        if (window != null) {
            window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        }
        getDialog().setCanceledOnTouchOutside(true);
    }

    @Override
    public void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    @Override
    public void setPayDialogUI(ProductInfo productInfo) {
        int price = productInfo.getPrice();
        int userGold = productInfo.getSnowGold();
        mPriceView.setText(ViewUtils.setStyleText(String.valueOf(price), "", mContext, "#d50000", "#d50000", 22, 14, "雪币"));
        if (userGold >= price) {
            mBuyButton.setText("立即支付");
            mBalanceView.setText(String.format(Locale.CHINESE, "余额： %d雪币", userGold));
        } else {
            mBuyButton.setText("充值并支付");
            mBalanceView.setText(ViewUtils.setStyleText("余额： " + userGold + "雪币", "", mContext, "#717171", "#d50000", 15, 15, "  余额不足"));
        }

        mBuyButton.setOnClickListener(view -> {
            if (userGold >= price) {
                mPresenter.buyMusicBySnowGold(mSongId);
            } else {
                Intent intent = new Intent(mContext, PayActivity.class);
                intent.putExtra("price", String.valueOf(price - userGold));
                intent.putExtra("productstype", IConstants.PRODUCT_SNOW_GOLD);
                intent.putExtra("products", String.valueOf(price - userGold));
                mContext.startActivity(intent);
            }
        });
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = @Tag(RxBusEventType.Pay.PAY_STATUS)
    )
    public void onRxbusPayStatus(Integer payStatus) {
        if (payStatus == 1){
            mPresenter.buyMusicBySnowGold(mSongId);
        }
    }
}
